package com.jingli.pile.converter;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jingli.pile.entity.*;
import com.jingli.pile.service.ChargingPileService;
import com.jingli.pile.service.SupEquipmentInfoService;
import com.jingli.pile.service.SupOperatorInfoService;
import com.jingli.pile.service.SupStationInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class OrderInfoConverter {
    private Logger logger = LoggerFactory.getLogger(OrderInfoConverter.class);
    @Autowired
    private SupEquipmentInfoService equipmentInfoService;
    @Autowired
    private SupOperatorInfoService operatorInfoService;
    @Autowired
    private ChargingPileService pileService;
    @Autowired
    private SupStationInfoService stationInfoService;

    public SupChargeOrderInfo convert(ChargingOrder order) {
        SupChargeOrderInfo orderInfo = new SupChargeOrderInfo();
        orderInfo.setConnectorId(order.getParklockid());
        ChargingPile chargingPile = pileService.getById(order.getParkid());
        QueryWrapper<SupOperatorInfo> operatorInfoQueryWrapper = new QueryWrapper<>();
        operatorInfoQueryWrapper.eq("user_id",chargingPile.getUserid());
        QueryWrapper<SupStationInfo> stationInfoQueryWrapper = new QueryWrapper<>();
        stationInfoQueryWrapper.eq("plot_id",chargingPile.getPlot());
        SupStationInfo stationInfo = stationInfoService.getOne(stationInfoQueryWrapper);
        orderInfo.setStationId(stationInfo.getStationId());
        orderInfo.setOperatorId(stationInfo.getOperatorId());
        orderInfo.setEquipmentId(order.getParkid());
        orderInfo.setEquipmentOwnerId(stationInfo.getOperatorId());
        orderInfo.setOrderNo(stationInfo.getOperatorId() + order.getOrdernumber());
        orderInfo.setStartSoc(0.0);
        orderInfo.setEndSoc(0.0);
        orderInfo.setStartTime(order.getStarttime());
        orderInfo.setEndTime(DateUtil.formatDateTime(order.getRealEndTime()));
        orderInfo.setTotalPower(order.getConsumePower().doubleValue());
        orderInfo.setPushTimeStamp(DateUtil.formatDateTime(new Date()));
        orderInfo.setTotalMoney(Double.valueOf(order.getOrdergold()));
        orderInfo.setTotalSeviceMoney(order.getServiceFee().doubleValue());
        orderInfo.setTotalElecMoney(order.getChargeFee().doubleValue());
        orderInfo.setStopReason(0);
        orderInfo.setStopDesc("用户手动停止充电");
        return orderInfo;
    }
}
